﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class Default3 : System.Web.UI.Page
{
    protected static PagedDataSource ps = new PagedDataSource();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind(0);
        }

    }
    public void bind(int currentpage)
    {
        SqlConnection sqlconn = new SqlConnection();
        string sqlconnstr = ConfigurationManager.ConnectionStrings["Plastic_companyConnectionString"].ToString();
        sqlconn.ConnectionString = sqlconnstr;
        sqlconn.Open();
        string sqlstr = "select * from product";
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, sqlconn);
        DataSet ds = new DataSet();
        da.Fill(ds, "product");
        ps.DataSource = ds.Tables["product"].DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 9;
        ps.CurrentPageIndex = currentpage;
        this.DataList1.DataSource = ps;
        this.DataList1.DataKeyField = "shop_sign";
        this.DataList1.DataBind();
    }
    protected void DataList1_ItemCommand1(object source, DataListCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "first":
                ps.CurrentPageIndex = 0;
                bind(ps.CurrentPageIndex);
                break;
            case "pre":
                ps.CurrentPageIndex = ps.CurrentPageIndex - 1;
                bind(ps.CurrentPageIndex);
                break;
            case "next":
                ps.CurrentPageIndex = ps.CurrentPageIndex + 1;
                bind(ps.CurrentPageIndex);
                break;
            case "last":
                ps.CurrentPageIndex = ps.PageCount - 1;
                bind(ps.CurrentPageIndex);
                break;
            case "search":
                if (e.Item.ItemType == ListItemType.Footer)
                {
                    int pagecount = int.Parse(ps.PageCount.ToString());
                    TextBox txtpage = e.Item.FindControl("TextBox1") as TextBox;
                    int mypagenum = 0;
                    if (!txtpage.Text.Equals(""))
                        mypagenum = Convert.ToInt32(txtpage.Text.ToString());
                    if (mypagenum <= 0 || mypagenum > pagecount)
                        Response.Write("<script>alert('请输入正确的页数!')</script>");
                    else
                        bind(mypagenum - 1);
                }
                break;
        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            Label Currentpage = e.Item.FindControl("Label6") as Label;
            Label pagecount = e.Item.FindControl("Label4") as Label;
            LinkButton firstpage = e.Item.FindControl("LinkButton1") as LinkButton;
            LinkButton prepage = e.Item.FindControl("LinkButton2") as LinkButton;
            LinkButton nextpage = e.Item.FindControl("LinkButton3") as LinkButton;
            LinkButton lastpage = e.Item.FindControl("LinkButton4") as LinkButton;
            //currentpage.Text = "1";
            Currentpage.Text = (ps.CurrentPageIndex + 1).ToString();

            pagecount.Text = ps.PageCount.ToString();
            if (ps.IsFirstPage)
            {
                firstpage.Enabled = false;
                prepage.Enabled = false;
            }
            if (ps.IsLastPage)
            {
                nextpage.Enabled = false;
                lastpage.Enabled = false;
            }

        }
    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}